package com.tgy;

/**
 * @Author: tgy
 * @Date: 2020-11-25 11:09
 */
public class CountingSort01 {

    public static void sort(int[] nums) {

        if (nums == null || nums.length <= 1) {
            return;
        }

        int min = nums[0],max = nums[0];

        for (int num : nums) {

            if (min > num) {

                min = num;
            }

            if (max < num) {

                max  = num;
            }
        }

        int len = max - min + 1;

        int[] ints = new int[len];

        for (int num : nums) {

            ints[num - min]++;
        }

        int index = 0;

        for (int i = 0; i < ints.length; i++) {

            int anInt = ints[i];

            while (anInt != 0) {

                nums[index] = min + i;
                anInt--;
                index++;
            }
        }

    }
}
